package com.cevent.yameng.web.mall.gateway.config;/**
 * Created by Cevent on 2020/10/4.
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author cevent
 * @description  跨域解决配置
 * @date 2020/10/4 14:15
 */
@Configuration
public class YaMengMallCORSConfiguration {
    //1.返回跨域的filter
    @Bean
    public CorsWebFilter corsWebFilter(){

        //2.使用reactive响应式包下的类
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource= new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration=new CorsConfiguration();

        //2.1配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        //允许携带cookie/token请求(credential允许证书)，进行跨域
        corsConfiguration.setAllowCredentials(true);

        //2.1任意路径都进行跨域配置
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);

        //3.返回跨域
        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}
